package abc;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HumpKey {
    private static JSONObject covertObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        JSONObject newObject = new JSONObject();
        Set<String> set = object.keySet();
        for (String key : set) {
            Object value = object.get(key);
            if (value instanceof JSONArray) {
                //数组
                value = covertArray(object.getJSONArray(key));
            } else if (value instanceof JSONObject) {
                //对象
                value = covertObject(object.getJSONObject(key));
            }
            //这个方法自己写的改成驼峰，也可以改成大写小写
            key = lineToHump(key);
            newObject.put(key, value);
        }
        return newObject;
    }

    private static JSONArray covertArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        JSONArray newArray = new JSONArray();
        for (int i = 0; i < array.size(); i++) {
            Object value = array.get(i);
            if (value instanceof JSONArray) {
                //数组
                value = covertArray(array.getJSONArray(i));
            } else if (value instanceof JSONObject) {
                //对象
                value = covertObject(array.getJSONObject(i));
            }
            newArray.add(value);
        }
        return newArray;
    }

    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Pattern linePattern = Pattern.compile("_(\\w)");
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args) {
        String s ="{" +
                "  \"product_code\":\"ENTERPRISE_PAY\"," +
                "  \"biz_scene\":\"ISV_STANDARD\"," +
                "  \"account_id\":\"2088000166406784\"," +
                "  \"name\":\"张三\"," +
                "  \"identity\":\"name@email.com\"," +
                "  \"identity_type\":\"ALIPAY_LOGON_ID\"," +
                "  \"agreement_no\":\"20214922001380716008\"," +
                "  \"account_quota\":{" +
                "    \"quota_dimension\":\"MONTH\"," +
                "    \"quota_total\":\"100000\"," +
                "    \"custom_begin_date\":\"yyyy-MM-dd HH:mm\"," +
                "    \"custom_end_date\":\"yyyy-MM-dd HH:mm\"" +
                "  }," +
                "  \"business_params\":{" +
                "    \"employee_id\":\"983748293742\"," +
                "    \"group_id_list\":[" +
                "      \"11\"" +
                "    ]," +
                "    \"fund_ext_info\":{" +
                "      \"fund_identity_type\":\"ALIPAY_USER_ID\"," +
                "      \"fund_identity\":\"name@email.com\"" +
                "    }" +
                "  }," +
                "  \"identity_verified_info\":{" +
                "    \"user_name\":\"张三\"," +
                "    \"cert_type\":\"IDENTITY_CARD\"," +
                "    \"cert_no\":\"D61DCDD393C885BBA0091B7B57A71208\"" +
                "  }" +
                "}";
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);


        System.out.println(covertObject(jsonObject));

    }
}
